package com.zzxz.practicaltrainingsys.mapper;

import com.zzxz.practicaltrainingsys.entity.ChatRecord;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface ChatRecordMapper {

    /**
     * 插入聊天记录
     */
    @Insert("INSERT INTO chat_record (username, real_name, user_type, content, chat_type, invite_code, create_time) " +
            "VALUES (#{username}, #{realName}, #{userType}, #{content}, #{chatType}, #{inviteCode}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertChatRecord(ChatRecord chatRecord);

    /**
     * 根据聊天类型和邀请码查询聊天记录（分页）
     */
    @Select("SELECT * FROM chat_record " +
            "WHERE chat_type = #{chatType} AND invite_code = #{inviteCode} " +
            "ORDER BY create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> findChatRecords(@Param("chatType") String chatType,
                                     @Param("inviteCode") String inviteCode,
                                     @Param("offset") int offset,
                                     @Param("limit") int limit);

    /**
     * 获取最近的聊天记录（用于新用户进入时显示历史消息）
     */
    @Select("SELECT * FROM chat_record " +
            "WHERE chat_type = #{chatType} AND invite_code = #{inviteCode} " +
            "ORDER BY create_time DESC " +
            "LIMIT #{limit}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> findRecentChatRecords(@Param("chatType") String chatType,
                                           @Param("inviteCode") String inviteCode,
                                           @Param("limit") int limit);

    /**
     * 统计聊天记录总数
     */
    @Select("SELECT COUNT(*) FROM chat_record " +
            "WHERE chat_type = #{chatType} AND invite_code = #{inviteCode}")
    int countChatRecords(@Param("chatType") String chatType,
                         @Param("inviteCode") String inviteCode);

    /**
     * 根据用户名查询用户的聊天记录
     */
    @Select("SELECT * FROM chat_record " +
            "WHERE username = #{username} " +
            "ORDER BY create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userType", column = "user_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "chatType", column = "chat_type"),
            @Result(property = "inviteCode", column = "invite_code"),
            @Result(property = "createTime", column = "create_time")
    })
    List<ChatRecord> findUserChatRecords(@Param("username") String username,
                                         @Param("offset") int offset,
                                         @Param("limit") int limit);

    /**
     * 删除指定时间之前的聊天记录（用于定期清理）
     */
    @Delete("DELETE FROM chat_record WHERE create_time < #{beforeTime}")
    int deleteChatRecordsBefore(@Param("beforeTime") String beforeTime);
}